
/* File name	: handle.c
 * Project 		: BKIT MCR
 * Version 		: 1.0
 * Creation Date: 07/07/2010
 * Tabsize		: 4
 * Author  		: BKIT4U                   
 * Copyright	: (c) 2010 BKIT HARDWARE CLUB - www.bkit4u.com
 * License		: Free.
 * Comments		: 
 * 
 */

/*====================================*/
/*  Include                           */
/*====================================*/
#include <avr/io.h>
#include "handle.h"
#include "../eeprom/eeprom.h"


/*====================================*/
/*  Function name: handle_init        */
/*  Parameter: null                   */
/*  Description: 1.configure hardware */
/*               for handle function  */
/*               2.Used for handle    */
/*                  Servo motor       */
/*====================================*/
unsigned int handle_center = 1500;
void handle_init(){

	DDRE  |=  _BV(PE3);

	// Timer/Counter 3 initialization
	// Clock source: System Clock
	// Clock value: 2000.000 kHz
	// Mode: Ph. & fr. cor. PWM top=ICR3
	// OC3A output: Non-Inv.
	TCCR3A =0x80;
	TCCR3B =0x12;
	TCNT3H =0x00;
	TCNT3L =0x00;
	ICR3H  =0x3E;
	ICR3L  =0x80;
	OCR3AH =0x00;
	OCR3AL =0x00;

}

/*====================================*/
/*  Function name: handle             */
/*  Parameter: _angle                 */
/*  Description: used to control Servo*/
/*               to turn to "_angle"  */
/*====================================*/
void handle(int _angle){
	RC_ANGLE = handle_center - HANDLE_STEP * _angle;
}
void save_handle_center ()
{
	eeprom_writebyte (HANDLE_CENTER_POS, 	handle_center);			//low byte
	eeprom_writebyte (HANDLE_CENTER_POS+1, 	handle_center>>8);		//hight byte
}
void read_handle_center ()
{
	handle_center = eeprom_readbyte (HANDLE_CENTER_POS+1);
	handle_center <<= 8;
	handle_center = handle_center + eeprom_readbyte (HANDLE_CENTER_POS);
	if (handle_center < 700)
		handle_center = 700;
	if (handle_center > 2300)
		handle_center = 2300;
}

/****************END FILE**************/

